// app.js
//cloudfuctions/login/login.js

App({
  onLaunch: function () {
    wx.cloud.init({
      env: "xiegang-3gf3n6ez27c3c8e1",
      traceUser: true,
    });
    //调用云函数
    wx.cloud.callFunction({
      name: "get_openId",
      success: (res) => {
        //获取用户openid
        this.globalData.user_openid = res.result.openid;
        // console.log(this.globalData.user_openid)

        //在数据库中查找用户是否已经登录过了
        wx.cloud
          .database()
          .collection("userInfo")
          .where({
            _openid: res.result.openid,
          })
          .get({
            success: (result) => {
              this.globalData.userInfo = result.data[0];
              this.globalData.user_openid = result.data[0]._openid;
              wx.showToast({
                title: this.globalData.user_openid,
              });
            },
          });
      },
    });
  },
  //全局数据
  globalData: {
    //用户openid
    user_openid: "",
    //用户信息
    userInfo: null,
  },
});
